package utl.mofen.dbHandler;


import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import utl.mofen.intf.IDBOperator;
import utl.mofen.jsonStreamHandler.HandlerSession;
import utl.mofen.objects.JSonStream;
import utl.mofen.objects.KeyStream;
import utl.mofen.script.SQLParser;
import utl.mofen.utils.DateUtil;


public class DBBaseHandler implements IDBHandler {
	protected IDBOperator dbOpera = null;
	protected IDBOperator sysDbOpera = null;	
	protected JSonStream resultJsonStream = null;
	protected SQLParser sqlParser = null;
	protected KeyStream dataKeyStream = null;
	protected KeyStream resultKeyStream = null;
	protected boolean isTrace = false;
	protected JsonObject config = null;
	protected HandlerSession session=null;

	
	public void setDBOperator(IDBOperator __dbOpera) {
		dbOpera = __dbOpera;	
	}
	
	public void writeTrace(String sTrace, String traceKey)
	{
		JsonObject json = resultJsonStream.getJSonObject();
		JsonObject jsonTrace = null;
		if (!json.has("trace"))
		{
			jsonTrace = new JsonObject();						
			json.add("trace", jsonTrace);
		}else
		{
			jsonTrace = json.get("trace").getAsJsonObject();
		}
		
		JsonArray keyTrace = null;
		if (!jsonTrace.has(traceKey))
		{
			keyTrace = new JsonArray();						
			jsonTrace.add(traceKey, keyTrace);
		}else
		{
			keyTrace = jsonTrace.get(traceKey).getAsJsonArray();
		}
		
		JsonObject once = new JsonObject();
		once.addProperty("data", sTrace);
		once.addProperty("time", DateUtil.format("yyyy-MM-dd HH:mm:ss.S"));
		keyTrace.add(once);		
		
	}
	
	public void writeTraceSQL(String sqlScript)
	{
		writeTrace(sqlScript, "sqls");		
	}

	public void setResultJSonStream(JSonStream __resultJsonStream) {
		resultJsonStream = __resultJsonStream;
		
	}

	public void setSQLParser(SQLParser __sqlParser) {
		sqlParser = __sqlParser;		
	}


	public void doAction() throws Exception {
		throw new Exception("不能使用subBaseHandler");	
	}

	public void setIsTrace(boolean __isTrace) {
		isTrace = __isTrace;		
	}



	public void setConfig(JsonObject __config) {
		config = __config;
		
	}



	public void setResultKeyStream(KeyStream __resultKeyStream) {
		resultKeyStream =__resultKeyStream;		
	}



	public void setDataKeyStream(KeyStream __dataKeyStream) {
		dataKeyStream=__dataKeyStream;		
	}

	public void setSession(HandlerSession session) {
		this.session = session; 
		
	}

	public void setSysOperator(IDBOperator __dbOpera) {
		this.sysDbOpera = __dbOpera;
		
	}
}
